package aorise.com.frame.common;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import aorise.com.frame.R;

public class DialogUtil {

	private Activity mActivity = null;
	private Dialog dialogs2;

	public DialogUtil(Activity activity) {
		this.mActivity = activity;
	}

	public Dialog alert(CharSequence title, CharSequence message) {

		Builder builder = new Builder(mActivity);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		Dialog dialog = builder.create();
		dialog.show();
		return dialog;
	}

	public Dialog alert(int title, int message) {

		Builder builder = new Builder(mActivity);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		Dialog dialog = builder.create();
		dialog.show();
		return dialog;
	}

	public Dialog confirm(CharSequence title, CharSequence message,
			DialogInterface.OnClickListener confirmListener,
			DialogInterface.OnClickListener cancelListener) {
		Builder builder = new Builder(mActivity);
		builder.setTitle(title);
		builder.setMessage(message);

		try {
			builder.setPositiveButton("确定", confirmListener);
		} catch (Exception e) {
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
		}
		try {
			builder.setNegativeButton("取消", cancelListener);
		} catch (Exception e) {
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
		}

		Dialog dialog = builder.create();
		dialog.show();
		return dialog;
	}

	public Dialog confirm(int title, int message,
			DialogInterface.OnClickListener confirmListener,
			DialogInterface.OnClickListener cancelListener) {
		Builder builder = new Builder(mActivity);
		builder.setTitle(title);
		builder.setMessage(message);
		try {
			builder.setPositiveButton("确定", confirmListener);
		} catch (Exception e) {
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
		}
		try {
			builder.setNegativeButton("取消", cancelListener);
		} catch (Exception e) {
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
		}

		Dialog dialog = builder.create();
		dialog.show();
		return dialog;
	}

	// 弹出自定义的窗体
	public Dialog showView(CharSequence title, View view,
			DialogInterface.OnClickListener confirmListener,
			DialogInterface.OnClickListener cancelListener) {
		Builder builder = new Builder(mActivity);
		if (title != "")
			builder.setTitle(title);
		builder.setView(view);

		try {
			builder.setPositiveButton("确定", confirmListener);
		} catch (Exception e) {
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
		}
		try {
			builder.setNegativeButton("取消", cancelListener);
		} catch (Exception e) {
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
		}

		Dialog dialog = builder.create();
		dialog.show();
		return dialog;
	}

	public Dialog showView(int title, View view,
			DialogInterface.OnClickListener confirmListener,
			DialogInterface.OnClickListener cancelListener) {
		Builder builder = new Builder(mActivity);
		if (title != 0)
			builder.setTitle(title);
		builder.setView(view);

		try {
			builder.setPositiveButton("确定", confirmListener);
		} catch (Exception e) {
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
		}
		try {
			builder.setNegativeButton("取消", cancelListener);
		} catch (Exception e) {
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
						}
					});
		}

		Dialog dialog = builder.create();
		dialog.show();
		return dialog;
	}

	// 弹出自定义的信息
	public Dialog showMsg(CharSequence title, CharSequence message) {
		Builder builder = new Builder(mActivity);
		if (title != "")
			builder.setTitle(title);
		builder.setMessage(message);
		Dialog dialog = builder.create();
		dialog.show();
		return dialog;
	}

	public Dialog showMsg(int title, int message) {
		Builder builder = new Builder(mActivity);
		if (title != 0)
			builder.setTitle(title);
		builder.setMessage(message);
		Dialog dialog = builder.create();
		dialog.show();
		return dialog;
	}

//	public Dialog showLoading(String message) {
//		SDProgressDialog dialog = new SDProgressDialog(mActivity,
//				R.style.dialogBase);
//		TextView txt = dialog.getmTxtMsg();
//		if (message != null && txt != null) {
//			txt.setText(message);
//		}
//		dialog.show();
//		dialog.setCancelable(true);
//		return dialog;
//	}

	public Dialog showDialogExitLogin(String message, String ok,
									  String no,OnClickListener paramOnClickListener1,
									  OnClickListener paramOnClickListener2) {
		View view = this.mActivity.getLayoutInflater().inflate(
				R.layout.with_exitlogin_dialog, null);

		dialogs2 = new Dialog(this.mActivity,
				R.style.transparentFrameWindowStyle);
		// 设置dialog进入时的动画效果，是渐变效果还是滑动效果
		// dialogs2 = new Dialog(this.mActivity,
		// R.style.ActionSheetDialogStyle);

		// 设置dialog显示的位置
		Window dialogWindow = dialogs2.getWindow();
		// dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		dialogWindow.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);

		dialogs2.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
		TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
		Button tv_ok = (Button) view.findViewById(R.id.tv_ok_exielogin);
		tv_message.setText(message.replace("<br>", "\r\n"));
		tv_ok.setText(ok);
		tv_cancel.setText(no);
		tv_ok.setOnClickListener(paramOnClickListener1);
		tv_cancel.setOnClickListener(paramOnClickListener2);
		dialogs2.setCanceledOnTouchOutside(false);
		dialogs2.show();
		return dialogs2;
	}

	// //信息靠左显示
//	public Dialog showwithtwoBtView(String titles, String message, String ok,
//			String no, String type, OnClickListener paramOnClickListener1,
//			OnClickListener paramOnClickListener2) {
//		View view = this.mActivity.getLayoutInflater().inflate(
//				R.layout.with_twobt_dialogs, null);
//
//		dialogs2 = new Dialog(this.mActivity,
//				R.style.transparentFrameWindowStyle);
//		// 设置dialog进入时的动画效果，是渐变效果还是滑动效果
//		// dialogs2 = new Dialog(this.mActivity,
//		// R.style.ActionSheetDialogStyle);
//
//		// 设置dialog显示的位置
//		Window dialogWindow = dialogs2.getWindow();
//		// dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
//		dialogWindow.setGravity(Gravity.CENTER);
//		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//		lp.x = 0;
//		lp.y = 0;
//		dialogWindow.setAttributes(lp);
//
//		dialogs2.setContentView(view, new LayoutParams(
//				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//		TextView text = (TextView) view.findViewById(R.id.textview1);
//		TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
//		TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
//		TextView tv_ok = (TextView) view.findViewById(R.id.tv_ok);
//		tv_ok.setTextColor(Color.BLACK);
//		text.setVisibility(View.VISIBLE);
//		text.setText(titles);
//		tv_message.setText(message.replace("<br>", "\r\n"));
//		tv_ok.setText(ok);
//		tv_cancel.setText(no);
//		tv_ok.setOnClickListener(paramOnClickListener1);
//		tv_cancel.setOnClickListener(paramOnClickListener2);
//
//		if (type.equals("1")) {
//			tv_ok.setText("升级");
//		}
//		dialogs2.setCanceledOnTouchOutside(false);
//		dialogs2.show();
//		return dialogs2;
//	}

	// 信息居中显示
	public Dialog showwithtwoBtViews(String titles, String message, String ok,
			String no, String type, OnClickListener paramOnClickListener1,
			OnClickListener paramOnClickListener2) {
		View view = this.mActivity.getLayoutInflater().inflate(
				R.layout.with_twobt_dialogss, null);

		dialogs2 = new Dialog(this.mActivity,
				R.style.transparentFrameWindowStyle);
		// 设置dialog进入时的动画效果，是渐变效果还是滑动效果
		// dialogs2 = new Dialog(this.mActivity,
		// R.style.ActionSheetDialogStyle);

		// 设置dialog显示的位置
		Window dialogWindow = dialogs2.getWindow();
		// dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		dialogWindow.setGravity(Gravity.CENTER);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);

		dialogs2.setContentView(view, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		TextView text = (TextView) view.findViewById(R.id.textview1);
		TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
		TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
		TextView tv_ok = (TextView) view.findViewById(R.id.tv_ok);
		tv_ok.setTextColor(Color.BLACK);
		text.setVisibility(View.VISIBLE);
		text.setText(titles);
		tv_message.setText(message.replace("<br>", "\r\n"));
		tv_ok.setText(ok);
		tv_cancel.setText(no);
		tv_ok.setOnClickListener(paramOnClickListener1);
		tv_cancel.setOnClickListener(paramOnClickListener2);

		if (type.equals("1")) {
			tv_ok.setText("升级");
		}
		dialogs2.setCanceledOnTouchOutside(false);
		dialogs2.show();
		return dialogs2;
	}

	// 信息居中显示+notitle
//	public Dialog showwithtwoBtViewss(String message, String ok, String no,
//			String type, OnClickListener paramOnClickListener1,
//			OnClickListener paramOnClickListener2) {
//		View view = this.mActivity.getLayoutInflater().inflate(
//				R.layout.with_twobt_notitledialog, null);
//
//		dialogs2 = new Dialog(this.mActivity,
//				R.style.transparentFrameWindowStyle);
//		// 设置dialog进入时的动画效果，是渐变效果还是滑动效果
//		// dialogs2 = new Dialog(this.mActivity,
//		// R.style.ActionSheetDialogStyle);
//
//		// 设置dialog显示的位置
//		Window dialogWindow = dialogs2.getWindow();
//		// dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
//		dialogWindow.setGravity(Gravity.CENTER);
//		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//		lp.x = 0;
//		lp.y = 0;
//		dialogWindow.setAttributes(lp);
//
//		dialogs2.setContentView(view, new LayoutParams(
//				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//		TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
//		TextView tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
//		TextView tv_ok = (TextView) view.findViewById(R.id.tv_ok);
//		tv_ok.setTextColor(Color.BLACK);
//		tv_message.setText(message.replace("<br>", "\r\n"));
//		tv_ok.setText(ok);
//		tv_cancel.setText(no);
//		tv_ok.setOnClickListener(paramOnClickListener1);
//		tv_cancel.setOnClickListener(paramOnClickListener2);
//
//		if (type.equals("1")) {
//			tv_ok.setText("升级");
//		}
//		dialogs2.setCanceledOnTouchOutside(false);
//		dialogs2.show();
//		return dialogs2;
//	}

//	public void showwithoneBtViews(String message) {
//		View view = this.mActivity.getLayoutInflater().inflate(
//				R.layout.with_onebt_voucherdialog, null);
//		dialogs2 = new Dialog(mActivity);
//		TextView text2 = (TextView) view.findViewById(R.id.tv_message);
//		TextView text1 = (TextView) view.findViewById(R.id.textview1);
//		text2.setVisibility(View.VISIBLE);
//		dialogs2.setCanceledOnTouchOutside(true);
//		dialogs2.requestWindowFeature(Window.FEATURE_NO_TITLE);
//		Window window = dialogs2.getWindow();
//		window.setBackgroundDrawableResource(android.R.color.transparent);
//		WindowManager.LayoutParams lp = window.getAttributes();
//		text1.setVisibility(View.VISIBLE);
//		window.setGravity(Gravity.FILL_HORIZONTAL | Gravity.FILL_VERTICAL);
//		dialogs2.setContentView(view, new LayoutParams(
//				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//		TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
//		ImageView imageView = (ImageView) view.findViewById(R.id.imagecha);
//		LinearLayout layout=(LinearLayout) view.findViewById(R.id.Layout1);
//		tv_message.setText(message.replace("<br>", "\n"));
//		layout.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//				dialogs2.dismiss();
//
//			}
//		});
//		window.setAttributes(lp);
//		dialogs2.show();
//	}

	public void dialogdismiss() {

		dialogs2.dismiss();

	}

}
